package m.jclecai.com.dialog;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.adapter.ViewPagerAdapter;
import m.jclecai.com.fragment.TZPOPDCFragment;
import m.jclecai.com.fragment.TZPOPFragment;
import m.jclecai.com.listener.ISelectPlayListener;

/**
 * Created by hp on 2017/4/21.
 */

public class BettingDialogFragment extends DialogFragment implements View.OnClickListener {
    private ISelectPlayListener listener;
    ViewPager vp_pop_tou;
    private TextView tv_ziyou;
    private TextView tv_duochuan;
    int itemCount = 0, minCount = 0;
    boolean check = true;
    String value = "";
    //当前选中的过关方式
    private int currentDialoItemCheck = 0;
    private boolean isDG = false;

    public BettingDialogFragment() {
    }

    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.color.br));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    TZPOPFragment tzpopFragment;
    TZPOPDCFragment tzpopdcFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guoguan_popuwindow, null);
        vp_pop_tou = (ViewPager) view.findViewById(R.id.vp_pop_tou);
        tv_ziyou = (TextView) view.findViewById(R.id.tv_ziyou);
        tv_duochuan = (TextView) view.findViewById(R.id.tv_duochuan);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tv_definite = (TextView) view.findViewById(R.id.tv_definite);
        tv_ziyou.setOnClickListener(this);
        tv_duochuan.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        tv_definite.setOnClickListener(this);
        List<Fragment> fragmentList = new ArrayList<>();
        tzpopFragment = new TZPOPFragment();
        tzpopFragment.setDG(isDG);
        tzpopFragment.setItemCount(itemCount);
        tzpopFragment.setMinCount(minCount);
        tzpopdcFragment = new TZPOPDCFragment();
        tzpopdcFragment.setDG(true);
        tzpopdcFragment.setItemCount(itemCount);
        tzpopdcFragment.setMinCount(minCount);
        //显示默认选中的过关方式
        if (currentDialoItemCheck == 0) {
            tzpopFragment.setDefCheck(check, value);
        } else {
            tzpopdcFragment.setDefCheck(check, value);
        }
        fragmentList.add(tzpopFragment);
        fragmentList.add(tzpopdcFragment);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager(), fragmentList);
        vp_pop_tou.setAdapter(viewPagerAdapter);
        vp_pop_tou.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                changeColorAccordPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        vp_pop_tou.setCurrentItem(currentDialoItemCheck);
        return view;
    }

    private void changeColorAccordPosition(int position) {
        tv_ziyou.setTextColor(Color.parseColor(position == 1 ? "#ff000000":"#Fb4e46"));
        tv_ziyou.setBackgroundColor(position == 1 ? Color.parseColor("#40000000"): Color.WHITE);

        tv_duochuan.setTextColor(Color.parseColor(position == 1 ?  "#Fb4e46":"#ff000000"));
        tv_duochuan.setBackgroundColor(position == 1 ?  Color.WHITE : Color.parseColor("#40000000"));
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        changeColorAccordPosition(currentDialoItemCheck);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                //取消
                dismiss();
                break;
            case R.id.tv_definite:
                dismiss();
                String value = vp_pop_tou.getCurrentItem() == 0 ? tzpopFragment.getData() : tzpopdcFragment.getData();
                if (listener != null)
                    listener.select(value, vp_pop_tou.getCurrentItem());
                break;
            case R.id.tv_ziyou:
                //自由单关
                vp_pop_tou.setCurrentItem(0);
                break;
            case R.id.tv_duochuan:
                //多串
                vp_pop_tou.setCurrentItem(1);
                break;
        }
    }

    public void setItemCount(int count) {
        this.itemCount = count;
    }

    public void setMinCount(int minCount) {
        this.minCount = minCount;
    }

    public void setListener(ISelectPlayListener listener) {
        this.listener = listener;
    }

    /**
     * 设置默认显示的方式和显示的值
     *
     * @param check
     * @param value
     * @param position
     */
    public void setDefCheck(boolean check, String value, int position) {
        this.check = check;
        this.value = value;
        this.currentDialoItemCheck = position;
    }

    public void setDG(boolean isDG) {
        this.isDG = isDG;
    }
}
